package com.weightx.weight;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import java.io.File;

/**
 * @program:
 * @description:
 * @create: 2023-07-03 13:43
 **/
@Configuration
public class WebMVCConfig implements WebMvcConfigurer {

    @Value("${upload.uploadDir}")
    private String uploadDir;   // 上传地址

    @Value("${upload.requestDir}")
    private String requestDir;  // 请求地址

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        File imgDir = new File(uploadDir);
        System.out.println(imgDir.exists());
        boolean flag = false;
        if(!imgDir.exists()){
            flag = imgDir.mkdirs();
        }
        if(flag)
            System.out.println("已成功创建资源upload目录:"+uploadDir);
        System.out.println("getAbsolutePath:"+imgDir.getAbsolutePath());
        System.out.println("getPath:"+imgDir.getPath());
        registry.addResourceHandler(requestDir)
                .addResourceLocations("file:"+imgDir.getAbsolutePath()+File.separator);
    }
}
